<?php
/**********************************/
/* 设置说明                       */
/*                                */
/* $sitetitle - 系统名称.         */
/* $filefolder - 管理目录.        */
/* $user - 用户名                 */
/* $pass - 密码                   */
/**********************************/
header('Content-Type: text/html; charset=utf-8');
session_start();
$filefolder = "/home/judge/data/";
$sitetitle = 'OJ数据管理系统';
$user = '873681136';
$pass = 'axyz10506914560';
$safe_num = 0;//设置多少次后禁止登陆，为0则不限制，建议为3-5
$mail = 'metowolf@163.com';//若有恶意登录，会发邮件到这个邮箱，前提是mail()函数可用！
$meurl = $_SERVER['PHP_SELF'];
$me = end(explode('/',$meurl));
if(isset($_REQUEST['op'])){
	$op = $_REQUEST['op'];
}else{
	$op = 'home';
}
if(isset($_REQUEST['folder'])){
	$folder = $_REQUEST['folder'];
}else{
	$folder = '';
}
while (preg_match('/\.\.\//',$folder)) $folder = preg_replace('/\.\.\//','/',$folder);
while (preg_match('/\/\//',$folder)) $folder = preg_replace('/\/\//','/',$folder);
if ($folder == '') {
    $folder = $filefolder;
}elseif ($filefolder != '') {
    if (!@ereg($filefolder,$folder)) {
        $folder = $filefolder;
    }  
}
$ufolder = $folder;
if(@$_SESSION['error'] > $safe_num && $safe_num !== 0){
	printerror('您已经被限制登陆！');
}

/****************************************************************/
/* 用户登录函数                                                 */
/*                                                              */
/* 需要浏览器开启Cookies才可使用                                */
/****************************************************************/

if (@$_COOKIE['user'] != $user || @$_COOKIE['pass'] != md5($pass)) {
	if (@$_REQUEST['user'] == $user && @$_REQUEST['pass'] == $pass) {
	    setcookie('user',$user,time()+60*60*24*1);
	    setcookie('pass',md5($pass),time()+60*60*24*1);
	} else {
		if (@$_REQUEST['user'] == $user || @$_REQUEST['pass']) $er = true;
		login(@$er);
	}
}


/****************************************************************/
/* function maintop()                                           */
/*                                                              */
/* 控制站点的样式和头部内容                                     */
/* $title -> 顶部标题 $showtop -> 是否显示头部菜单              */
/****************************************************************/

function maintop($title,$showtop = true) {
	# 添加全局变量
    global $me,$sitetitle, $lastsess, $login, $viewing, $iftop, $user, $pass, $password, $debug, $issuper;
    echo "<html>\n<head>\n"
        ."<title>$sitetitle :: $title</title>\n"
        ."</head>\n"
        ."<body>\n"
        ."<style>\n"
        ."*{font-family:'微软雅黑' !important}"
        ."td{font-size:13px;font-family:'微软雅黑',Tahoma, Verdana, Arial, Helvetica, sans-serif;}\n"
        ."span{line-height:20px;}"
        ."a:visited{color:#333;text-decoration: none;}\n"
        ."a:hover {color:#666;text-decoration: none;}\n"
        ."a:link {color:#333;text-decoration: none;}\n"
        ."a:active {color:#666;text-decoration: none;}\n"
        ."table,form{width:700px !important;max-width:700px !important;}\n"
        ."textarea{font-family:'微软雅黑',Tahoma, Verdana, Arial, Helvetica, sans-serif;border:1px solid #ccc;margin:5px 0;padding:5px;width:700px;border-radius:2px;}\n"
        ."input.button{margin:5px 0;font-size:13px;*font-size:90%;*overflow:visible;padding:4px 10px;;color:#fff !important;color: white !important;*color:#fff !important;border:1px solid #fff;border:0 rgba(0,0,0,0);background-color:#666;text-decoration:none;border-radius:2px}\n"
        ."input.button:hover{filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#00000000', endColorstr='#1a000000', GradientType=0);background-image:-webkit-gradient(linear,0 0,0 100%,from(transparent),color-stop(40%,rgba(0,0,0,.05)),to(rgba(0,0,0,.1)));background-image:-webkit-linear-gradient(transparent,rgba(0,0,0,.05) 40%,rgba(0,0,0,.1));background-image:-moz-linear-gradient(top,rgba(0,0,0,.05) 0,rgba(0,0,0,.1));background-image:-o-linear-gradient(transparent,rgba(0,0,0,.05) 40%,rgba(0,0,0,.1));background-image:linear-gradient(transparent,rgba(0,0,0,.05) 40%,rgba(0,0,0,.1));text-decoration: none}"
        ."input.buuton:active{box-shadow:0 0 0 1px rgba(0,0,0,.15) inset,0 0 6px rgba(0,0,0,.2) inset}"
        ."input.text,.upload{border: 1px solid #999;height:27px;margin:6px 1px;padding:5px;;font-size:12px;border-radius:2px;}\n"
        ."body{;background-color:#ededed;font-family:'微软雅黑',Tahoma, Verdana, Arial, Helvetica, sans-serif; margin: 0px 0px 10px;}\n"
        .".title{font-weight: bold; FONT-SIZE: 12px;text-align: center;}\n"
        .".error{font-size:10pt;color:#AA2222;text-align:left}\n"
        .".menu{border-top:1px solid #999;border-bottom:1px solid #999;font-size:13px;padding:5px;margin-bottom:15px;}.menu a{text-decoration:none;margin-right:8px;}\n"
        .".table{background-color:#777;color:#fff;}"
        .".mytable tr:hover{background:#ededed;color:#469;font-size:13px;}"
        .".table:hover{background-color:#777 !important;color:#fff !important}"
        ."tr{height:26px;}.upload{width:400px;margin-left:-40px;}"
        ."</style>\n\n";

    if ($viewing == "") {#Table表格的开始
        echo "<table cellpadding=10 cellspacing=10 bgcolor=#ededed align=center><tr><td>\n"
            ."<table cellpadding=1 cellspacing=1><tr><td>\n"
            ."<table cellpadding=5 cellspacing=5 bgcolor='white'><tr><td>\n";
    }else{
        echo "<table cellpadding=7 cellspacing=7 bgcolor='white'><tr><td>\n";
    }

    echo "<h2>$sitetitle <small>:: $title</small></h2>\n";

    if ($showtop) {//头部菜单内容
        echo "<div class=\"menu\">\n"
            ."<a href=\"".$_SERVER['SCRIPT_NAME']."?op=home\" $iftop>主页</a>\n"
            ."<a href=\"".$_SERVER['SCRIPT_NAME']."?op=up\" $iftop>上传</a>\n"
            ."<a href=\"".$_SERVER['SCRIPT_NAME']."?op=cr\" $iftop>创建</a>\n"
            ."<a href=\"".$_SERVER['SCRIPT_NAME']."?op=allz\" $iftop>全站备份</a>\n"
            ."<a href=\"".$_SERVER['SCRIPT_NAME']."?op=sqlb\" $iftop>数据库备份</a>\n"
            ."<a href=\"".$_SERVER['SCRIPT_NAME']."?op=ftpa\" $iftop>FTP功能</a>\n"
            ."<a href=\"".$_SERVER['SCRIPT_NAME']."?op=killme&dename=".$me."&folder=./\" onclick=\"return confirm('你确定要自杀吗？');\">自杀</a>\n"
            ."<a href=\"".$_SERVER['SCRIPT_NAME']."?op=logout\" $iftop>退出</a>\n"
            ."</div>";
    }
}


/****************************************************************/
/* function login()                                             */
/*                                                              */
/* 登录验证 $user and md5($pass)                                */
/* 需要浏览器支持Cookie                                         */
/****************************************************************/

function login($er=false) {
    global $op,$safe_num,$mail;
    setcookie("user","",time()-60*60*24*1);
    setcookie("pass","",time()-60*60*24*1);
    maintop("登录",false);

    if ($er) { 
        if (isset($_SESSION['error'])){
            $_SESSION['error']++;
            if($_SESSION['error'] > $safe_num && $safe_num !== 0){
                @mail($mail,'FileBox文件管理器提醒：文件被恶意登录！','该提醒来自FileBox：<br>登录者IP为：'.$_SERVER['REMOTE_ADDR'],'From: <i@hezii.pw>');
                printerror('您已经被限制登陆！');
            }
        }else{
            $_SESSION['error'] = 1;
        }
        echo "<span class=error>**错误: 不正确的登录信息.**</span><br><br>\n"; 
    }

    echo "<form action=\"".$_SERVER['SCRIPT_NAME']."?op=".$op."\" method=\"post\">\n"
        ."<input type=\"text\" name=\"user\" border=\"0\" class=\"text\" value=\"".@$user."\"  placeholder=\"请输入用户名\">\n"
        ."<input type=\"password\" name=\"pass\" border=\"0\" class=\"text\" value=\"".@$pass."\" placeholder=\"请输入密码\"><br>\n"
        ."<input type=\"submit\" name=\"submitButtonName\" value=\"登录\" border=\"0\" class=\"button\">\n"
        ."</form>\n";
    mainbottom();
}


/****************************************************************/
/* function home()                                              */
/*                                                              */
/* Main function that displays contents of folders.             */
/****************************************************************/

function home() {
    global $folder, $ufolder,$filefolder, $HTTP_HOST;
    maintop("主页");
    echo "<table border=\"0\" cellpadding=\"2\" cellspacing=\"0\" width=100% class='mytable'><form method='post'>\n";

    $content1 = "";
    $content2 = "";

    $count = "0";
    $folder = iconv("UTF-8", "GBK", $folder);
    $style = opendir($folder);
    $a=1;
    $b=1;

    if ($folder) {
        $_SESSION['folder']=$ufolder;
      }

    while($stylesheet = readdir($style)) {
    if ($stylesheet !== "." && $stylesheet !== ".." ) {
        if (is_dir($folder.$stylesheet) && is_readable($folder.$stylesheet)) {
            $sstylesheet = $stylesheet;
            $stylesheet = iconv("GBK", "UTF-8", $stylesheet);
            $ulfolder = $folder;
            $folder = iconv("GBK", "UTF-8", $folder);
            $content1[$a] = "<td><input name='select_item[d][$stylesheet]' type='checkbox' id='$stylesheet' onclick='One($stylesheet)' class=\"checkbox\" value='".$folder.$stylesheet."' /></td>\n"
                           ."<td><a href=\"".$_SERVER['SCRIPT_NAME']."?op=home&folder=".$folder.$stylesheet."/\">".$stylesheet."</a></td>\n"
                           ."<td></td>"
                         //.disk_total_space($folder.$stylesheet)." 由于某些问题注释掉这段代码
                           ."<td align=\"center\"><a href=\"".$_SERVER['SCRIPT_NAME']."?op=home&folder=".$folder.$stylesheet."/\">打开</a></td>\n"
                           ."<td align=\"center\"><a href=\"".$_SERVER['SCRIPT_NAME']."?op=ren&file=".$stylesheet."&folder=$folder\">重命名</a></td>\n"
                           ."<td align=\"center\"><a href=\"".$_SERVER['SCRIPT_NAME']."?op=z&dename=".$stylesheet."&folder=$folder\">压缩</a></td>\n"
                           ."<td align=\"center\">".substr(sprintf('%o',fileperms($ulfolder.$sstylesheet)), -3)."</td>\n";
            $a++;
            $folder = iconv("UTF-8", "GBK", $folder);
        }elseif(!is_dir($folder.$stylesheet) && is_readable($folder.$stylesheet)){ 
            if(filesize($folder.$stylesheet) < 1024){
                $filesize = filesize($folder.$stylesheet)."B";
            }elseif(filesize($folder.$stylesheet) > 1024 and filesize($folder.$stylesheet) < 1024*1024){
                $count1 = round(filesize($folder.$stylesheet)/1024,1);
                $filesize = $count1."K";
            }elseif(filesize($folder.$stylesheet) > 1024*1024 and filesize($folder.$stylesheet) < 1024*1024*1024){
                $count1 = round(filesize($folder.$stylesheet)/1024/1024,1);
                $filesize = $count1."M";
            }

        if(preg_match ("/.zip$/i", $folder.$stylesheet)){#判断是否是zip文件
            $sstylesheet = $stylesheet;
            $ulfolder = $folder;
            $stylesheet = iconv("GBK", "UTF-8", $stylesheet);
            $folder = iconv("GBK", "UTF-8", $folder);
            $content2[$b] = "<td><input name='select_item[f][$stylesheet]' type='checkbox' id='$stylesheet' class=\"checkbox\" value='".$folder.$stylesheet."' /></td>\n"
                           ."<td><a href=\"".$folder.$stylesheet."\" target='_blank'>".$stylesheet."</a></td>\n"
                           ."<td align=\"left\">".$filesize
                           ."<td align=\"center\"></td>\n"
                           ."<td align=\"center\"><a href=\"".$_SERVER['SCRIPT_NAME']."?op=ren&file=".$stylesheet."&folder=$folder\">重命名</a>\n"
                           ."<td align=\"center\"><a href=\"".$_SERVER['SCRIPT_NAME']."?op=unz&dename=".$stylesheet."&folder=$folder\">提取</a>\n"
                           ."<td align=\"center\">".substr(sprintf('%o',fileperms($ulfolder.$sstylesheet)), -3)."</a>\n";
            $b++;
            $folder = iconv("UTF-8", "GBK", $folder);
        }else{
            $sstylesheet = $stylesheet;
            $ulfolder = $folder;
            $stylesheet = iconv("GBK", "UTF-8", $stylesheet);
            $folder = iconv("GBK", "UTF-8", $folder);
            $content2[$b] = "<td><input name='select_item[f][$stylesheet]' type='checkbox' id='$stylesheet' class=\"checkbox\" value='".$folder.$stylesheet."' /></td>\n"
                           ."<td><a href=\"".$folder.$stylesheet."\" target='_blank'>".$stylesheet."</a></td>\n"
                           ."<td align=\"left\">".$filesize
                           ."<td align=\"center\"><a href=\"".$_SERVER['SCRIPT_NAME']."?op=edit&fename=".$stylesheet."&folder=$folder\">编辑</a>\n"
                           ."<td align=\"center\"><a href=\"".$_SERVER['SCRIPT_NAME']."?op=ren&file=".$stylesheet."&folder=$folder\">重命名</a>\n"
                           ."<td align=\"center\"><a href=\"".$folder.$stylesheet."\" target='_blank'>查看</a>\n"
                           ."<td align=\"center\">".substr(sprintf('%o',fileperms($ulfolder.$sstylesheet)), -3)."</a>\n";
            $b++;
            $folder = iconv("UTF-8", "GBK", $folder);
        }
    }
    $count++;
    } 
}
    closedir($style);

    echo "浏览目录: $ufolder\n"
         ."<br>文件数: " . $count . "<br><br>";

    echo "<tr class='table' width=100%>"
        ."<script>"
        ."function Check() {
            var collid = document.getElementById(\"check\")
            var coll = document.getElementsByTagName('input')
            if (collid.checked){
                for(var i = 0; i < coll.length; i++)
                    coll[i].checked = true;
            }else{
                for(var i = 0; i < coll.length; i++)
                    coll[i].checked = false;
            }
         }"
       ."</script>"
       ."<td width=20px></td>\n"
       ."<td width=300>文件名</td>\n"
       ."<td width=65>大小</td>\n"
       ."<td align=\"center\" width=44>打开</td>\n"
       ."<td align=\"center\" width=58>重命名</td>\n"
       ."<td align=\"center\" width=45>操作</td>\n"
       ."<td align=\"center\" width=45>权限</td>\n"
       ."</tr>";
    if($ufolder!=="./"){
        $count = substr_count($ufolder,"/");
        $last = explode('/', $ufolder);
        $i = 1;
        $back = ".";
        while($i < $count-1){
              $back = $back."/".$last[$i];
              $i++;
        }
        echo "<tr width=100%><td></td><td><a href=\"".$_SERVER['SCRIPT_NAME']."?op=home&folder=".$back."/"."\">上级目录</a></td><td></td><td></td><td></td><td></td><td></td></tr>";
    }
    for ($a=1; $a<count($content1)+1;$a++) {
        $tcoloring   = ($a % 2) ? '#DEDEDE' : '#ededed';
        if(empty($content1)){
        }else{
            echo "<tr width=100%>";
            echo @$content1[$a];
        }
    }

    for ($b=1; $b<count($content2)+1;$b++) {
        $tcoloring   = ($a++ % 2) ? '#DEDEDE' : '#ededed';
        echo "<tr width=100%>";
        echo @$content2[$b];
    }

    echo "</table><div><input type=\"checkbox\" id=\"check\" onclick=\"Check()\"> <input class='button' name='action' type='submit' value='移动' /> <input class='button' name='action' type='submit' value='复制' /> <input class='button' name='action' type='submit' onclick=\"return confirm('选中的文件将压缩在本目录的backup.zip文件下，确定要这么做吗?')\"  value='压缩' /> <input class='button' name='action' type='submit' onclick=\"return confirm('您真的要删除选中的文件吗?')\" value='删除' /> <input class='button' name='action' type='submit' onclick=\"var t=document.getElementById('chmod').value;return confirm('将这些文件的权限修改为'+t+'？如果是文件夹，将会递归文件夹内所有内容！')\" value='权限' /> <input type=\"text\" class=\"text\" stlye=\"vertical-align:text-top;\" size=\"3\" id=\"chmod\" name=\"chmod\" value=\"0755\"></div></form>";
    mainbottom();
}


/****************************************************************/
/* function up()                                                */
/*                                                              */
/* First step to Upload.                                        */
/* User enters a file and the submits it to upload()            */
/****************************************************************/

function up() {
    global $folder, $content, $filefolder;
    maintop("上传");

    echo "<FORM ENCTYPE=\"multipart/form-data\" ACTION=\"".$_SERVER['SCRIPT_NAME']."?op=upload\" METHOD=\"POST\">\n"
        ."<h3>本地上传 | 最大可以上传".ini_get('upload_max_filesize')."的文件</h3>文件:                                                    上传目录:<br><input type=\"File\" name=\"upfile[]\" multiple size=\"20\">\n"
        ."<input type=\"text\" name=\"ndir\" value=\"".$_SESSION["folder"]."\" class=\"upload\">\n";

    echo $content
        ."</select><br>"
        ."<input type=\"submit\" value=\"上传\" class=\"button\">\n"
        ."<script>"
        ."function UpCheck(){
      	  if(document.getElementById(\"unzip\").checked){
          document.getElementById(\"deluzip\").disabled=false;
          }else{
          document.getElementById(\"deluzip\").disabled=true;}
          }"
        ."</script>"
        ."<input type=\"checkbox\" name=\"unzip\" id=\"unzip\" value=\"checkbox\" onclick=\"UpCheck()\" checked><abbr title='提取（解压）上传的Zip压缩文件'>解压</abbr> "
        ."<input type=\"checkbox\" name=\"delzip\" id=\"deluzip\"value=\"checkbox\"><abbr title='同时将上传的压缩文件删除'>删除</abbr>"
        ."</form>\n";
    echo "<h3>远程上传</h3>远程上传是什么意思？<br>远程上传是从其他服务器获取文件并直接下载到当前服务器的一种功能。<br>类似于SSH的Wget功能，免去我们下载再手动上传所浪费的时间。<br><br><form action=\"".$_SERVER['SCRIPT_NAME']."?op=yupload\" method=\"POST\"><input name=\"url\" size=\"85\" type=\"text\" class=\"text\" placeholder=\"请输入文件地址...\"/> <input type=\"text\" class=\"text\" size=\"20\" name=\"ndir\" value=\"".$_SESSION["folder"]."\">"
         ."<input name=\"submit\" value=\"上传\" type=\"submit\" class=\"button\"/>\n"
         ."<script>"
         ."function Check(){
      	  if(document.getElementById(\"un\").checked){
          document.getElementById(\"del\").disabled=false;
          }else{
          document.getElementById(\"del\").disabled=true;}
          }"
         ."</script>"
         ."<input type=\"checkbox\" name=\"unzip\" id=\"un\" value=\"checkbox\" onclick=\"Check()\" checked><abbr title='提取（解压）上传的Zip压缩文件'>解压</abbr> "
         ."<input type=\"checkbox\" name=\"delzip\" id=\"del\"value=\"checkbox\"><abbr title='同时将上传的压缩文件删除'>删除</abbr></form>";

    mainbottom();
}


/****************************************************************/
/* function yupload()                                           */
/*                                                              */
/* Second step in wget file.                                    */
/* Saves the file to the disk.                                  */
/* Recieves $upfile from up() as the uploaded file.             */
/****************************************************************/

function yupload($url, $folder, $unzip, $delzip) {
    $nfolder = $folder;
    $url = iconv("UTF-8", "GBK", $url);
    $folder = iconv("UTF-8", "GBK", $folder);
    if($url!==""){
        set_time_limit (24 * 60 * 60); // 设置超时时间
  	    if (!file_exists($folder)){
    	    mkdir($folder, 0755);
        }
    $newfname = $folder . basename($url); // 取得文件的名称
    $file = fopen ($url, "rb"); // 远程下载文件，二进制模式
    if ($file) { // 如果下载成功
        $newf = fopen ($newfname, "wb");
    if ($newf) // 如果文件保存成功
        while (!feof($file)) { // 判断附件写入是否完整
        fwrite($newf, fread($file, 1024 * 8), 1024 * 8); // 没有写完就继续
        }
    }
    if ($file) {
        fclose($file); // 关闭远程文件
    }
    if ($newf) {
        fclose($newf); // 关闭本地文件
    }
    maintop("远程上传");
    echo "文件 ".basename($url)." 上传成功<br>\n";
    if(end(explode('.', basename($url)))=="zip" && isset($unzip) && $unzip == "checkbox"){
        if(class_exists('ZipArchive')){
            $zip = new ZipArchive();
            if ($zip->open($folder.basename($url)) === TRUE) {
                $zip->extractTo($folder);
                $zip->close();
                echo basename($nurl)." 已经被解压到$nfolder<br>";
                if(isset($delzip) && $delzip == "checkbox"){
            	    if(unlink($folder.basename($url))){
            	        echo basename($url)." 删除成功<br>";
                    }else{
            	        echo basename($url)." 删除失败<br>";
                }
                    echo "你可以 <a href=\"".$_SERVER['SCRIPT_NAME']."?op=home&folder=".$folder."\">访问文件夹</a> 或者 <a href=\"".$_SERVER['SCRIPT_NAME']."?op=home&folder=".$_SESSION['folder']."\">返回文件管理</a>  或者 <a href=\"".$_SERVER['SCRIPT_NAME']."?op=up\">继续上传</a>\n";
                }
            }else{
                echo('<span class="error">无法解压文件：'.$nfolder.basename($nurl).'</span><br>');
            }
        }else{
        	echo('<span class="error">此服务器上的PHP不支持ZipArchive，无法解压文件！</span><br>');
        }
    }else{
    	echo "你可以 <a href=\"".$_SERVER['SCRIPT_NAME']."?op=home&folder=".$nfolder."\">访问文件夹</a> 或者 <a href=\"".$_SERVER['SCRIPT_NAME']."?op=edit&fename=".basename($url)."&folder=".$nfolder."\">编辑文件</a> 或者 <a href=\"".$_SERVER['SCRIPT_NAME']."?op=home&folder=".$_SESSION['folder']."\">返回文件管理</a>  或者 <a href=\"".$_SERVER['SCRIPT_NAME']."?op=up\">继续上传</a>\n";
    }
    mainbottom();
    return true;
    }else{
	    printerror ('文件地址不能为空。');
    }
}


/****************************************************************/
/* function upload()                                            */
/*                                                              */
/* Second step in upload.                                       */
/* 将文件保存到磁盘中                                           */
/* Recieves $upfile from up() as the uploaded file.             */
/****************************************************************/

function upload($upfile,$ndir,$unzip,$delzip) {
    global $folder;
    $nfolder = $folder;
    $nndir = $ndir;
    $ndir = iconv("UTF-8", "GBK", $ndir);
    if (!$upfile) {
        printerror("您没有选择文件！");
    }elseif($upfile) { 
  	    maintop("上传");
  	if (!file_exists($ndir)){
    	mkdir($ndir, 0755);
    }
    $i = 1;
    while (count($upfile['name']) >= $i){
    	$dir = iconv("UTF-8", "GBK", $nndir.$upfile['name'][$i-1]);
        if(copy($upfile['tmp_name'][$i-1],$dir)) {
            echo "文件 ".$nndir.$upfile['name'][$i-1]." 上传成功\n<br>";
            if(end(explode('.', $upfile['name'][$i-1]))=="zip" && isset($unzip) && $unzip == "checkbox"){
            	if(class_exists('ZipArchive')){
                    $zip = new ZipArchive();
                    if ($zip->open($dir) === TRUE) {
                        $zip->extractTo($ndir);
                        $zip->close();
                        echo $upfile['name'][$i-1]." 已经被解压到$nndir<br>";
                        if(isset($delzip) && $delzip == "checkbox"){
            	            unlink($folder.$dename);
            	            echo $upfile['name'][$i-1]." 删除成功<br>";
                        }
                    }else{
                        echo("<span class=\"error\">无法解压文件：".$nndir.$upfile['name'][$i-1]."</span><br>");
                    }
                }else{
            	    echo("<span class=\"error\">此服务器上的PHP不支持ZipArchive，无法解压文件！</span><br>");
                }
            }
        }else{
            echo("<span class=\"error\">文件 ".$upfile['name'][$i-1]." 上传失败</span><br>");
        }
        $i++;
    }
        echo "你可以 <a href=\"".$_SERVER['SCRIPT_NAME']."?op=home&folder=".$ndir."\">打开文件夹</a> 或者 <a href=\"".$_SERVER['SCRIPT_NAME']."?op=home&folder=".$_SESSION['folder']."\">返回文件管理</a> 或者 <a href=\"".$_SERVER['SCRIPT_NAME']."?op=up\">继续上传</a>\n";
        mainbottom();
    }else{
        printerror("您没有选择文件！");
    }
}


/****************************************************************/
/* function allz()                                               */
/*                                                              */
/* First step in allzip.                                        */
/* Prompts the user for confirmation.                           */
/* Recieves $dename and ask for deletion confirmation.          */
/****************************************************************/

function allz() {
    maintop("全站备份");
    echo "<table border=\"0\" cellpadding=\"2\" cellspacing=\"0\">\n"
        ."<span class='error'>**警告: 这将进行全站打包成allbackup.zip的动作! 如存在该文件，该文件将被覆盖!**</span><br><br>\n"
        ."确定要进行全站打包?<br><br>\n"
        ."你可以 <a href=\"".$_SERVER['SCRIPT_NAME']."?op=allzip\">我已经了解该操作所造成的后果，确认使用</a> 或者 \n"
        ."<a href=\"".$_SERVER['SCRIPT_NAME']."?op=home&folder=".$_SESSION['folder']."\">返回文件管理</a>\n"
        ."</table>\n";
    mainbottom();
}


/****************************************************************/
/* function allzip()                                            */
/*                                                              */
/* Second step in unzip.                                       */
/****************************************************************/

function allzip() {
    if(class_exists('ZipArchive')){
    	maintop("全站备份");
        if (file_exists('allbackup.zip')) {
            unlink('allbackup.zip'); 
        }
        class Zipper extends ZipArchive {
            public function addDir() {
                $nodes = glob('./*');
                foreach ($nodes as $node) {
                	$node = substr($node, 2);
                	$nnode = iconv("GBK", "UTF-8", $node);
                    echo $nnode.'<br>';
                    if (is_dir($node)) {
                        $this->addDir2($node);
                    }elseif(is_file($node)){
                        $this->addFile($node);
                    }
                }
            }
            public function addDir2($path) {
            	$npath = iconv("GBK", "UTF-8", $path);
                echo $npath . '<br>';
                $this->addEmptyDir($path);
                $nodes = glob($path . '/*');
                foreach ($nodes as $node) {
                	$nnode = iconv("GBK", "UTF-8", $node);
                    echo $nnode.'<br>';
                    if (is_dir($node)) {
                        $this->addDir2($node);
                    }elseif(is_file($node)){
                        $this->addFile($node);
                    }
                }
            }
        }
        $zip = new Zipper;
        $res = $zip->open('allbackup.zip', ZipArchive::CREATE);
        if ($res === TRUE) {
            $zip->addDir();
            $zip->close();
            echo '全站压缩完成！'
                ." <a href=\"".$_SERVER['SCRIPT_NAME']."?op=home&folder=".$_SESSION['folder']."\">返回文件管理</a>\n";
        }else{
            echo '<span class="error">全站压缩失败！</span>'
                ." <a href=\"".$_SERVER['SCRIPT_NAME']."?op=home&folder=".$_SESSION['folder']."\">返回文件管理</a>\n";
        }
        mainbottom();
    }else{
    	printerror('此服务器上的PHP不支持ZipArchive，无法压缩文件！');
    }
}


/****************************************************************/
/* function unz()                                               */
/*                                                              */
/* First step in unz.                                        */
/* Prompts the user for confirmation.                           */
/* Recieves $dename and ask for deletion confirmation.          */
/****************************************************************/

function unz($dename) {
    global $folder, $content, $filefolder;
    if (!$dename == "") {
        maintop("解压");
        echo "<table border=\"0\" cellpadding=\"2\" cellspacing=\"0\">\n"
            ."<span class=error>**警告: 这将解压 ".$folder.$dename.". **</span ><br><br>\n"
            ."<form ENCTYPE=\"multipart/form-data\" action=\"".$_SERVER['SCRIPT_NAME']."?op=unzip\">解压到..."
            ."<input type=\"text\" name=\"ndir\" class=\"text\" value=\"".$_SESSION['folder']."\">";
        echo $content
            ."</select>"
            ."<br><br>确定要解压 ".$folder.$dename."?<br><br>\n"
            ."<input type=\"hidden\" name=\"op\" value=\"unzip\">\n"
            ."<input type=\"hidden\" name=\"dename\" value=\"".$dename."\">\n"
            ."<input type=\"hidden\" name=\"folder\" value=\"".$folder."\">\n"
            ."<input type=\"submit\" value=\"解压\" class=\"button\"><input type=\"checkbox\" name=\"del\" id=\"del\"value=\"del\">同时删除压缩文件<br><br>\n"
            ." <a href=\"".$_SERVER['SCRIPT_NAME']."?op=home&folder=".$_SESSION['folder']."\">返回文件管理</a>\n"
            ."</table>\n";
        mainbottom();
    }else{
        home();
    }
}


/****************************************************************/
/* function unzip()                                            */
/*                                                              */
/* Second step in unzip.                                       */
/****************************************************************/
function unzip($dename,$ndir,$del) {
    global $folder;
    $nndir = $ndir;
    $nfolder = $folder;
    $ndename = $dename;
    $dename = iconv("UTF-8", "GBK", $dename);
    $folder = iconv("UTF-8", "GBK", $folder);
    $ndir = iconv("UTF-8", "GBK", $ndir);
    if (!$dename == "") {
        if (!file_exists($ndir)){
    	    mkdir($ndir, 0755);
        }
        if(class_exists('ZipArchive')){
            $zip = new ZipArchive();
            if ($zip->open($folder.$dename) === TRUE) {
                $zip->extractTo($ndir);
                $zip->close();
                maintop("解压");
                echo $dename." 已经被解压到 $nndir<br>";
                if($del=='del'){
                	unlink($folder.$dename);
                	echo $ndename." 已经被删除<br>";
                }
                echo "<a href=\"".$_SERVER['SCRIPT_NAME']."?op=home&folder=".$_SESSION['folder']."\">返回文件管理</a>\n";
                mainbottom();
            }else{
                printerror('无法解压文件：'.$nfolder.$ndename);
            }
        }else{
        	printerror('此服务器上的PHP不支持ZipArchive，无法解压文件！');
        }
    }else{
        home();
    }
}


/****************************************************************/
/* function delete()                                            */
/*                                                              */
/* Second step in delete.                                       */
/* Deletes the actual file from disk.                           */
/* Recieves $upfile from up() as the uploaded file.             */
/****************************************************************/

function deltree($pathdir)  
{  
if(is_empty_dir($pathdir))//如果是空的  
    {  
    rmdir($pathdir);//直接删除  
    }  
    else  
    {//否则读这个目录，除了.和..外  
        $d=dir($pathdir);  
        while($a=$d->read())  
        {  
        if(is_file($pathdir.'/'.$a) && ($a!='.') && ($a!='..')){unlink($pathdir.'/'.$a);}  
        //如果是文件就直接删除  
        if(is_dir($pathdir.'/'.$a) && ($a!='.') && ($a!='..'))  
        {//如果是目录  
            if(!is_empty_dir($pathdir.'/'.$a))//是否为空  
            {//如果不是，调用自身，不过是原来的路径+他下级的目录名  
            deltree($pathdir.'/'.$a);  
            }  
            if(is_empty_dir($pathdir.'/'.$a))  
            {//如果是空就直接删除  
            rmdir($pathdir.'/'.$a);
            }
        }  
        }  
        $d->close();  
    }  
}  

function is_empty_dir($pathdir)  
{ 
//判断目录是否为空 
    $d=opendir($pathdir);  
    $i=0;  
    while($a=readdir($d)){  
        $i++;  
    }  
    closedir($d);  
    if($i>2){return false;}  
    else return true;  
    }


/****************************************************************/
/* function edit()                                              */
/*                                                              */
/* First step in edit.                                          */
/* Reads the file from disk and displays it to be edited.       */
/* Recieves $upfile from up() as the uploaded file.             */
/****************************************************************/

function edit($fename) {
    global $folder;
    if (!$fename == "") {
    maintop("编辑");
    echo $folder.$fename;

    echo "<form action=\"".$_SERVER['SCRIPT_NAME']."?op=save\" method=\"post\">\n"
        ."<textarea cols=\"73\" rows=\"40\" name=\"ncontent\">\n";

    $file = iconv("UTF-8", "GBK", $folder.$fename);
    $handle = fopen ($file, "r");
    $contents = "";

    while (@$x<1) {
        $data = @fread ($handle, filesize ($file));
        if (strlen($data) == 0) {
          break;
        }
        $contents .= $data;
    }
    fclose ($handle);

    $replace1 = "</text";
    $replace2 = "area>";
    $replace3 = "< / text";
    $replace4 = "area>";
    $replacea = $replace1.$replace2;
    $replaceb = $replace3.$replace4;
    $contents = preg_replace ($replacea,$replaceb,$contents);

    echo $contents;

    echo "</textarea>\n"
        ."<br>\n"
        ."<input type=\"hidden\" name=\"folder\" value=\"".$folder."\">\n"
        ."<input type=\"hidden\" name=\"fename\" value=\"".$fename."\">\n"
        ."<input type=\"submit\" value=\"保存\" class=\"button\"> <a href=\"".$_SERVER['SCRIPT_NAME']."?op=home&folder=".$_SESSION['folder']."\">返回文件管理</a>\n"
        ."</form>\n";
    mainbottom();
    }else{
    home();
    }
}


/****************************************************************/
/* function save()                                              */
/*                                                              */
/* Second step in edit.                                         */
/* Recieves $ncontent from edit() as the file content.          */
/* Recieves $fename from edit() as the file name to modify.     */
/****************************************************************/

function save($ncontent, $fename) {
    global $folder;
    if (!$fename == "") {
    maintop("编辑");
    $file = iconv("UTF-8", "GBK", $folder.$fename);
    $fp = fopen($file, "w");

    $replace1 = "</text";
    $replace2 = "area>";
    $replace3 = "< / text";
    $replace4 = "area>";
    $replacea = $replace1.$replace2;
    $replaceb = $replace3.$replace4;
    $ncontent = preg_replace ($replaceb,$replacea,$ncontent);
    $ydata = stripslashes($ncontent);

    if(fwrite($fp, $ydata)) {
        echo "文件 <a href=\"".$folder.$fename."\" target=\"_blank\">".$folder.$fename."</a> 保存成功！\n"
            ." <a href=\"".$_SERVER['SCRIPT_NAME']."?op=home&folder=".$_SESSION['folder']."\">返回文件管理</a> 或者 <a href=\"".$_SERVER['SCRIPT_NAME']."?op=edit&fename=".$fename."&folder=".$folder."\">继续编辑</a>\n";
        $fp = null;
    }else{
        echo "<span class='error'>文件保存出错！</span>\n"
        ." <a href=\"".$_SERVER['SCRIPT_NAME']."?op=home&folder=".$_SESSION['folder']."\">返回文件管理</a>\n";
    }
    mainbottom();
    }else{
    home();
    }
}


/****************************************************************/
/* function cr()                                                */
/*                                                              */
/* First step in create.                                        */
/* Promts the user to a filename and file/directory switch.     */
/****************************************************************/

function cr() {
    global $folder, $content, $filefolder;
    maintop("创建");
    if (!$content == "") { echo "<br><br>请输入一个名称.\n"; }
    echo "<form action=\"".$_SERVER['SCRIPT_NAME']."?op=create\" method=\"post\">\n"
        ."文件名：<br><input type=\"text\" size=\"20\" name=\"nfname\" class=\"text\"><br><br>\n"
        ."目标目录：<br><input type=\"text\" class=\"text\" name=\"ndir\" value=\"".$_SESSION['folder']."\">";
    echo $content
        ."</select><br><br>";

    echo "文件 <input type=\"radio\" size=\"20\" name=\"isfolder\" value=\"0\" checked><br>\n"
        ."目录 <input type=\"radio\" size=\"20\" name=\"isfolder\" value=\"1\"><br><br>\n"
        ."<input type=\"hidden\" name=\"folder\" value=\"$folder\">\n"
        ."<input type=\"submit\" value=\"创建\" class=\"button\">  <a href=\"".$_SERVER['SCRIPT_NAME']."?op=home&folder=".$_SESSION['folder']."\">返回文件管理</a>\n"
        ."</form>\n";
    mainbottom();
}


/****************************************************************/
/* function create()                                            */
/*                                                              */
/* Second step in create.                                       */
/* Creates the file/directoy on disk.                           */
/* Recieves $nfname from cr() as the filename.                  */
/* Recieves $infolder from cr() to determine file trpe.         */
/****************************************************************/

function create($nfname, $isfolder, $ndir) {
    global $folder;
    if (!$nfname == "") {
        maintop("创建");
        $ndir = iconv("UTF-8", "GBK", $ndir);
        $nfname = iconv("UTF-8", "GBK", $nfname);
    if ($isfolder == 1) {
        if(@mkdir($ndir."/".$nfname, 0755)) {
        	$ndir = iconv("GBK", "UTF-8", $ndir);
        	$nfname = iconv("GBK", "UTF-8", $nfname);
            echo "您的目录<a href=\"".$_SERVER['SCRIPT_NAME']."?op=home&folder=./".$nfname."/\">".$ndir.$nfname."/</a> 已经成功被创建.\n"
            ."<br><a href=\"".$_SERVER['SCRIPT_NAME']."?op=home&folder=".$ndir.$nfname."/\">打开文件夹</a> | <a href=\"".$_SERVER['SCRIPT_NAME']."?op=home&folder=".$_SESSION['folder']."\">返回文件管理</a>\n";
        }else{
        	$ndir = iconv("GBK", "UTF-8", $ndir);
        	$nfname = iconv("GBK", "UTF-8", $nfname);
            echo "<span class='error'>您的目录".$ndir."".$nfname." 不能被创建。请检查您的目录权限是否已经被设置为可写 或者 目录是否已经存在</span>\n";
        }
    }else{
        if(@fopen($ndir."/".$nfname, "w")) {
        	$ndir = iconv("GBK", "UTF-8", $ndir);
        	$nfname = iconv("GBK", "UTF-8", $nfname);
            echo "您的文件, <a href=\"".$_SERVER['SCRIPT_NAME']."?op=viewframe&file=".$nfname."&folder=$ndir\">".$ndir.$nfname."</a> 已经成功被创建\n"
                ."<br><a href=\"".$_SERVER['SCRIPT_NAME']."?op=edit&fename=".$nfname."&folder=".$ndir."\">编辑文件</a> | <a href=\"".$_SERVER['SCRIPT_NAME']."?op=home&folder=".$_SESSION['folder']."\">返回文件管理</a>\n";
        }else{
        	$ndir = iconv("GBK", "UTF-8", $ndir);
        	$nfname = iconv("GBK", "UTF-8", $nfname);
            echo "<span class='error'>您的文件 ".$ndir.$nfname." 不能被创建。请检查您的目录权限是否已经被设置为可写 或者 文件是否已经存在</span>\n";
        }
    }
    mainbottom();
    }else{
    cr();
    }
}


/****************************************************************/
/* function ren()                                               */
/*                                                              */
/* First step in rename.                                        */
/* Promts the user for new filename.                            */
/* Globals $file and $folder for filename.                      */
/****************************************************************/

function ren($file) {
    global $folder,$ufolder;
    $ufile = $file;
    if (!$file == "") {
        maintop("重命名");
        echo "<form action=\"".$_SERVER['SCRIPT_NAME']."?op=rename\" method=\"post\">\n"
            ."<table border=\"0\" cellpadding=\"2\" cellspacing=\"0\">\n"
            ."重命名 ".$ufolder.$ufile;
        echo "</table><br>\n"
            ."<input type=\"hidden\" name=\"rename\" value=\"".$ufile."\">\n"
            ."<input type=\"hidden\" name=\"folder\" value=\"".$ufolder."\">\n"
            ."新文件名:<br><input class=\"text\" type=\"text\" size=\"20\" name=\"nrename\" value=\"$ufile\">\n"
            ."<input type=\"Submit\" value=\"重命名\" class=\"button\"></form>\n";
        mainbottom();
    }else{
        home();
    }
}


/****************************************************************/
/* function renam()                                             */
/*                                                              */
/* Second step in rename.                                       */
/* Rename the specified file.                                   */
/* Recieves $rename from ren() as the old  filename.            */
/* Recieves $nrename from ren() as the new filename.            */
/****************************************************************/

function renam($rename, $nrename, $folder) {
    global $folder;
    if (!$rename == "") {
        $loc1 = iconv("UTF-8", "GBK", "$folder".$rename); 
        $loc2 = iconv("UTF-8", "GBK", "$folder".$nrename);
        if(rename($loc1,$loc2)) {
        	maintop("重命名");
            echo "文件 ".$folder.$rename." 已被重命名成 ".$folder.$nrename."</a>\n"
            ." <a href=\"".$_SERVER['SCRIPT_NAME']."?op=home&folder=".$_SESSION['folder']."\">返回文件管理</a>\n";
            mainbottom();
        }else{
            printerror("重命名出错！");
        }
    }else{
    home();
    }
}

/****************************************************************/
/* function movall                                              */
/*                                                              */
/* 批量移动 2014-4-12 by jooies                                 */
/****************************************************************/

function movall($file, $ndir, $folder) {
    global $folder;
    if (!$file == "") {
        maintop("批量移动");
        $nndir = $ndir;
        $nfolder = $folder;
    	$file = iconv("UTF-8", "GBK",$file);
    	$ndir = iconv("UTF-8", "GBK",$ndir);
    	$folder = iconv("UTF-8", "GBK",$folder);
        if (!file_exists($ndir)){
    	    mkdir($ndir, 0755);
        }
        $file = explode(',',$file);
        foreach ($file as $v) {
        if (file_exists($ndir.$v)){
        	@unlink($ndir.$v);
        	if (@rename($folder.$v, $ndir.$v)){
        		$v = iconv("GBK", "UTF-8",$v);
    	        echo $nndir.$v." 文件被 ".$nfolder.$v." 替换<br>";
            }else{
            	$v = iconv("GBK", "UTF-8",$v);
                echo "<span class='error'>无法移动 ".$nfolder.$v.'，请检查文件权限</span><br>';
            }
        }elseif (@rename($folder.$v, $ndir.$v)){
        	$v = iconv("GBK", "UTF-8",$v);
            echo $nfolder.$v." 已经成功移动到 ".$nndir.$v.'<br>';
        }else{
        	$v = iconv("GBK", "UTF-8",$v);
            echo "<span class='error'>无法移动 ".$nfolder.$v.'，请检查文件权限</span><br>';
        }
        }
    echo "你可以 <a href=\"".$_SERVER['SCRIPT_NAME']."?op=home&folder=".$nndir."\">前往文件夹查看文件</a> 或者 <a href=\"".$_SERVER['SCRIPT_NAME']."?op=home&folder=".$_SESSION['folder']."\">返回文件管理</a>\n";
    mainbottom();
    }else{
    home();
    }
}

/****************************************************************/
/* function tocopy                                              */
/*                                                              */
/* 批量复制 2014-4-19 by jooies                                 */
/****************************************************************/

function tocopy($file, $ndir, $folder) {
    global $folder;
    if (!$file == "") {
        maintop("复制");
        $nndir = $ndir;
        $nfolder = $folder;
    	$file = iconv("UTF-8", "GBK",$file);
    	$ndir = iconv("UTF-8", "GBK",$ndir);
    	$folder = iconv("UTF-8", "GBK",$folder);
        if (!file_exists($ndir)){
    	    mkdir($ndir, 0755);
        }
        $file = explode(',',$file);
        foreach ($file as $v) {
        if (file_exists($ndir.$v)){
        	@unlink($ndir.$v);
        	if (@copy($folder.$v, $ndir.$v)){
        		$v = iconv("GBK", "UTF-8",$v);
    	        echo $nndir.$v." 文件被 ".$nfolder.$v." 替换<br>";
            }else{
            	$v = iconv("GBK", "UTF-8",$v);
                echo "<span class='error'>无法复制 ".$nfolder.$v.'，请检查文件权限</span><br>';
            }
        }elseif (@copy($folder.$v, $ndir.$v)){
        	$v = iconv("GBK", "UTF-8",$v);
            echo $nfolder.$v." 已经成功复制到 ".$nndir.$v.'<br>';
        }else{
        	$v = iconv("GBK", "UTF-8",$v);
            echo "<span class='error'>无法复制 ".$nfolder.$v.'，请检查文件权限</span><br>';
        }
        }
    echo "你可以 <a href=\"".$_SERVER['SCRIPT_NAME']."?op=home&folder=".$nndir."\">前往文件夹查看文件</a> 或者 <a href=\"".$_SERVER['SCRIPT_NAME']."?op=home&folder=".$_SESSION['folder']."\">返回文件管理</a>\n";
    mainbottom();
    }else{
    home();
    }
}


/****************************************************************/
/* function logout()                                            */
/*                                                              */
/* Logs the user out and kills cookies                          */
/****************************************************************/

function logout() {
    global $login;
    setcookie("user","",time()-60*60*24*1);
    setcookie("pass","",time()-60*60*24*1);

    maintop("退出",false);
    echo "你已经退出."
        ."<br><br>"
        ."<a href=".$_SERVER['SCRIPT_NAME']."?op=home>点击这里重新登录.</a>";
    mainbottom();
}


/****************************************************************/
/* function mainbottom()                                        */
/*                                                              */
/* 页面底部的版权声明                                           */
/****************************************************************/

function mainbottom() {
    echo "</table></table>\n"
        ."\n<div style='text-align:center'>"
        ."雪狼网 数据管理系统 1.5.0 </div></table></table></body>\n"
        ."</html>\n";
    exit;
}


/****************************************************************/
/* function sqlb()                                              */
/*                                                              */
/* First step to backup sql.                                    */
/****************************************************************/

function sqlb() {
    maintop("数据库备份");
    echo @$content 
        ."<table border=\"0\" cellpadding=\"2\" cellspacing=\"0\"></table><span class='error'>**警告: 这将进行数据库导出并压缩成mysql.zip的动作! 如存在该文件,该文件将被覆盖!**</span><br><br><form action=\"".$_SERVER['SCRIPT_NAME']."?op=sqlbackup\" method=\"POST\">数据库地址:  <input name=\"ip\" size=\"30\" class=\"text\"/><br>数据库名称:  <input name=\"sql\" size=\"30\" class=\"text\"/><br>数据库用户:  <input name=\"username\" size=\"30\" class=\"text\"/><br>数据库密码:  <input name=\"password\" size=\"30\" class=\"text\"/><br>数据库编码:  <select id=\"chset\"><option id=\utf8\">utf8</option></select><br><input name=\"submit\" class=\"button\" value=\"备份\" type=\"submit\" /></form>\n";
    mainbottom();
}


/****************************************************************/
/* function sqlbackup()                                         */
/*                                                              */
/* Second step in backup sql.                                   */
/****************************************************************/

function sqlbackup($ip,$sql,$username,$password) {
    if(class_exists('ZipArchive')){
    maintop("数据库备份");
    $database=$sql;//数据库名
    $options=array(
        'hostname' => $ip,//ip地址
        'charset' => 'utf8',//编码
        'filename' => $database.'.sql',//文件名
        'username' => $username,
        'password' => $password
    );
    mysql_connect($options['hostname'],$options['username'],$options['password'])or die("不能连接数据库!");
    mysql_select_db($database) or die("数据库名称错误!");
    mysql_query("SET NAMES '{$options['charset']}'");
    $tables = list_tables($database);
    $filename = sprintf($options['filename'],$database);
    $fp = fopen($filename, 'w');
    foreach ($tables as $table) {
        dump_table($table, $fp);
    }
    fclose($fp);
    //压缩sql文件
        if (file_exists('mysql.zip')) {
            unlink('mysql.zip'); 
        }
        $file_name=$options['filename'];
        $zip = new ZipArchive;
        $res = $zip->open('mysql.zip', ZipArchive::CREATE);
        if ($res === TRUE) {
            $zip->addfile($file_name);
            $zip->close();
        //删除服务器上的sql文件
            unlink($file_name);
        echo '数据库导出并压缩完成！'
            ." <a href=\"".$_SERVER['SCRIPT_NAME']."?op=home&folder=".$_SESSION['folder']."\">返回文件管理</a>\n";
        }else{
            printerror('无法压缩文件！');
        }
    exit;
    mainbottom();
    }else{
    	printerror('此服务器上的PHP不支持ZipArchive，无法压缩文件！');
    }
}

function list_tables($database)
{
    $rs = mysql_list_tables($database);
    $tables = array();
    while ($row = mysql_fetch_row($rs)) {
        $tables[] = $row[0];
    }
    mysql_free_result($rs);
    return $tables;
}

//导出数据库
function dump_table($table, $fp = null)
{
    $need_close = false;
    if (is_null($fp)) {
        $fp = fopen($table . '.sql', 'w');
        $need_close = true;
    }
$a=mysql_query("show create table `{$table}`");
$row=mysql_fetch_assoc($a);fwrite($fp,$row['Create Table'].';');//导出表结构
    $rs = mysql_query("SELECT * FROM `{$table}`");
    while ($row = mysql_fetch_row($rs)) {
        fwrite($fp, get_insert_sql($table, $row));
    }
    mysql_free_result($rs);
    if ($need_close) {
        fclose($fp);
    }
}

//导出表数据
function get_insert_sql($table, $row)
{
    $sql = "INSERT INTO `{$table}` VALUES (";
    $values = array();
    foreach ($row as $value) {
        $values[] = "'" . mysql_real_escape_string($value) . "'";
    }
    $sql .= implode(', ', $values) . ");";
    return $sql;
}

function z($dename,$folder) {
    global $folder;
    maintop("目录压缩");
    echo "<table border=\"0\" cellpadding=\"2\" cellspacing=\"0\">\n"
        ."<span class=error>**警告: 这将进行目录压缩为".$dename.".zip的动作! 如存在该文件，该文件将被覆盖!**</span><br><br>\n"
        ."确定要将此目录进行压缩?压缩后文件将保存在".$_SESSION['folder']."$dename.zip<br><br>\n"
        ."你可以 <a href=\"".$_SERVER['SCRIPT_NAME']."?op=zip&dename=".$dename."&folder=$folder\">我已经了解该操作所造成的后果，确认使用</a> 或者 \n"
        ." <a href=\"".$_SERVER['SCRIPT_NAME']."?op=home&folder=".$_SESSION['folder']."\">返回文件管理</a>\n"
        ."</table>\n";
    mainbottom();
}

function zip($dename,$folder) {
    global $folder;
    $npath = $folder.$dename;
    $path = iconv("UTF-8", "GBK", $folder.$dename);
    if(class_exists('ZipArchive')){
    maintop("目录压缩");
    if (file_exists($dename.'.zip')) {
    unlink($dename.'.zip'); }

    class Zipper extends ZipArchive {
        public function addDir($path) {
        	$npath = iconv("GBK", "UTF-8", $path);
            echo '<span>'.$npath.'</span><br>';
            $this->addEmptyDir($path);
            $nodes = glob($path . '/*');
            foreach ($nodes as $node) {
            	$nnode = iconv("GBK", "UTF-8", $node);
                echo '<span>'.$nnode.'</span><br>';
                if (is_dir($node)) {
                	$node = substr($node, 2);
                    $this->addDir2($node);
                }elseif(is_file($node))  {
                	$node = substr($node, 2);
                    $this->addFile($node);
                }
            }
        }
    }

    $zip = new Zipper;
    $res = $zip->open(substr($path, 1).'.zip', ZipArchive::CREATE);
    if ($res === TRUE) {
        $zip->addDir($path);
        $zip->close();
        echo '<span>压缩完成！</span>'
            ."<br>你可以 <a href=\"".$_SERVER['SCRIPT_NAME']."?op=home&folder=".$npath."/\">查看压缩的文件</a> 或者 <a href=\"".$_SERVER['SCRIPT_NAME']."?op=home&folder=".$_SESSION['folder']."\">返回文件管理</a>\n";
    }else{
        echo '<span class="error">压缩失败！</span>'
            ." <a href=\"".$_SERVER['SCRIPT_NAME']."?op=home&folder=".$_SESSION['folder']."\">返回文件管理</a>\n";
    }
    }
    else{printerror('此服务器上的PHP不支持ZipArchive，无法压缩文件！');}
    mainbottom();
}



function killme($dename) {
    global $folder;
    if (!$dename == "") {
        if(unlink($folder.$dename)) {
        	maintop("自杀");
            echo "自杀成功！ "
                ." <a href=".$folder.">返回网站首页</a>\n";
            mainbottom();
        }else{
            printerror("自杀失败，请检查文件权限！");
        }    }else{
        home();
    }
}


/****************************************************************/
/* function ftpa()                                              */
/*                                                              */
/* First step to backup sql.                                    */
/****************************************************************/

function ftpa() {
    maintop("FTP功能");
    echo @$content
        ."<table border=\"0\" cellpadding=\"2\" cellspacing=\"0\"></table><span class='error'>**警告: 这将把文件远程上传到其他ftp! 如目录存在该文件,文件将被覆盖!**</span><br><br><form action=\"".$_SERVER['SCRIPT_NAME']."?op=ftpall\" method=\"POST\">FTP 地址:  <input name=\"ftpip\" size=\"30\" class=\"text\" value=\"127.0.0.1:21\"/><br>FTP 用户:  <input name=\"ftpuser\" size=\"30\" class=\"text\"/><br>FTP 密码:  <input name=\"ftppass\" size=\"30\" class=\"text\"/><br>上传目录:  <input name=\"goto\" size=\"30\" class=\"text\" value=\"./htdocs/\"/><br>上传文件:  <input name=\"ftpfile\" size=\"30\" class=\"text\" value=\"allbackup.zip\"/><br><input name=\"submit\" class=\"button\" value=\"远程上传\" type=\"submit\" /><input type=\"checkbox\" name=\"del\" id=\"del\"value=\"checkbox\"><abbr title='FTP上传后删除本地文件'>删除</abbr></form>\n";
    mainbottom();
}


/****************************************************************/
/* function ftpall()                                         */
/*                                                              */
/* Second step in backup sql.                                   */
/****************************************************************/

function ftpall($ftpip,$ftpuser,$ftppass,$ftpdir,$ftpfile,$del) {
	$ftpfile = iconv("UTF-8", "GBK", $ftpfile);
    maintop("FTP上传");
    $ftpip=explode(':', $ftpip);
    $ftp_server=$ftpip['0'];//服务器
    $ftp_user_name=$ftpuser;//用户名
    $ftp_user_pass=$ftppass;//密码
    if(empty($ftpip['1'])){
    	$ftp_port='21';
    }else{
    	$ftp_port=$ftpip['1'];//端口
    }
    $ftp_put_dir=$ftpdir;//上传目录
    $ffile=$ftpfile;//上传文件

    $ftp_conn_id = ftp_connect($ftp_server,$ftp_port);
    $ftp_login_result = ftp_login($ftp_conn_id, $ftp_user_name, $ftp_user_pass);

    if((!$ftp_conn_id) || (!$ftp_login_result)) {
        echo "连接到ftp服务器失败";
        exit;
    }else{
        ftp_pasv ($ftp_conn_id,true); //返回一下模式，这句很奇怪，有些ftp服务器一定需要执行这句
        ftp_chdir($ftp_conn_id, $ftp_put_dir);
        $ffile=explode(',', $ffile);
        foreach ($ffile as $v) {
        	$ftp_upload = ftp_put($ftp_conn_id,$v,$v, FTP_BINARY);
        	if ($del == 'del') {
        		unlink('./'.$v);
        	}
        }
        ftp_close($ftp_conn_id); //断开
    }

    $ftpfile = iconv("GBK", "UTF-8", $ftpfile);
    echo "文件 ".$ftpfile." 上传成功\n"
        ." <a href=\"".$_SERVER['SCRIPT_NAME']."?op=home&folder=".$_SESSION['folder']."\">返回文件管理</a>\n";
    mainbottom();
}


/****************************************************************/
/* function printerror()                                        */
/*                                                              */
/* 用于显示错误信息的函数                                       */
/* $error为显示的提示                                           */
/****************************************************************/

function printerror($error) {
    maintop("错误");
    echo "<span class=error>\n".$error."\n</span>"
        ." <a onclick=\"history.go(-1);\" style=\"cursor:pointer\">返回上一步</a>\n";
    mainbottom();
}

/****************************************************************/
/* function deleteall()                                         */
/*                                                              */
/* 2014-3-9 Add by Jooies                                       */
/* 实现文件的批量删除功能                                       */
/****************************************************************/

function deleteall($dename) {
    if (!$dename == "") {
    	$udename = $dename;
    	$dename = iconv("UTF-8", "GBK",$dename);
        if (is_dir($dename)) {
            if(is_empty_dir($dename)){ 
                rmdir($dename);
                echo "<span>".$udename." 已经被删除</span><br>";
            }else{
                deltree($dename);
                rmdir($dename);
                echo "<span>".$udename." 已经被删除</span><br>";
            }
        }else{
            if(@unlink($dename)) {
                echo '<span>'.$udename." 已经被删除</span><br>";
            }else{
                echo("<span class='error'>无法删除文件：$udename ，可能是文件不存在！</span><br>");
            }
        }
    }
}

if(@$_POST['action']=='删除'){
    if(isset($_POST['select_item'])){
    	maintop("删除");
        if(@$_POST['select_item']['d']){
            foreach($_POST['select_item']['d'] as $val){
                deleteall($val);
            }
        }
        if(@$_POST['select_item']['f']){
            foreach($_POST['select_item']['f'] as $val){
                if(deleteall($val)){}
            }
        }
        echo "<a href=\"".$_SERVER['SCRIPT_NAME']."?op=home&folder=".$_SESSION['folder']."\">返回文件管理</a>\n";
        mainbottom();
    }else{
        printerror("您没有选择文件");
    }
}

if(@$_POST['action']=='移动'){
    if(isset($_POST['select_item'])){
    	maintop("批量移动");
    	$file = '';
        if(@$_POST['select_item']['d']){
            foreach($_POST['select_item']['d'] as $key => $val){
                $file = $file.$key.',';
            }
        }
        if(@$_POST['select_item']['f']){
            foreach($_POST['select_item']['f'] as $key => $val){
                $file = $file.$key.',';
            }
        }
        $file = substr($file,0,-1);
    	echo "<form action=\"".$_SERVER['SCRIPT_NAME']."?op=movall\" method=\"post\">";
    	echo '<input type="hidden" name="file" value="'.$file.'"><input type="hidden" name="folder" value="'.$_SESSION['folder'].'">您将把下列文件移动到：'
    	    ."<input type=\"text\" class=\"text\" name=\"ndir\" value=\"".$_SESSION['folder']."\"><br>\n"
    	    .$file;
        echo "<br><input type=\"submit\" value=\"移动\" border=\"0\" class=\"button\"> <a href=\"".$_SERVER['SCRIPT_NAME']."?op=home&folder=".$_SESSION['folder']."\">返回文件管理</a>\n";
        mainbottom();
    }else{
        printerror("您没有选择文件");
    }
}

if(@$_POST['action']=='复制'){
    if(isset($_POST['select_item'])){
    	maintop("复制");
    	$file = '';
        if(@$_POST['select_item']['d']){
            foreach($_POST['select_item']['d'] as $key => $val){
                $file = $file.$key.',';
            }
        }
        if(@$_POST['select_item']['f']){
            foreach($_POST['select_item']['f'] as $key => $val){
                $file = $file.$key.',';
            }
        }
        $file = substr($file,0,-1);
    	echo "<form action=\"".$_SERVER['SCRIPT_NAME']."?op=copy\" method=\"post\">";
    	echo '<input type="hidden" name="file" value="'.$file.'"><input type="hidden" name="folder" value="'.$_SESSION['folder'].'">您将把下列文件复制到：'
    	    ."<input type=\"text\" class=\"text\" name=\"ndir\" value=\"".$_SESSION['folder']."\"><br>\n"
    	    .$file;
        echo "<br><input type=\"submit\" value=\"复制\" border=\"0\" class=\"button\"> <a href=\"".$_SERVER['SCRIPT_NAME']."?op=home&folder=".$_SESSION['folder']."\">返回文件管理</a>\n";
        mainbottom();
    }else{
        printerror("您没有选择文件");
    }
}

if(@$_POST['action']=='压缩'){
    if(isset($_POST['select_item'])){
    if(class_exists('ZipArchive')){
    	maintop("目录压缩");
        if (file_exists('backup.zip')) {
            unlink('backup.zip'); 
        }
        class Zipper extends ZipArchive {
            public function addDir($path) {
                $this->addEmptyDir($path);
                if(@$_POST['select_item']['d']){
                    foreach($_POST['select_item']['d'] as $key => $val){
                    	$val = substr($val,2);
                    	$val = iconv("UTF-8", "GBK",$val);
                    	$nodes = glob('./'.$val.'*');
                    	foreach ($nodes as $node) {
                    		$node = substr($node,2);
                    		if(is_dir($node)){
                    			$this->addDir2($node);
                    		}else{
                    			$this->addFile($node);
                    		}
                    	}
                    }
                }
                if(@$_POST['select_item']['f']){
                    foreach($_POST['select_item']['f'] as $key => $val){
                    	$val = substr($val,2);
                    	echo $val.'<br>';
                        $this->addFile($val);
                    }
                }
            }
            public function addDir2($path) {
                $nval = iconv("GBK", "UTF-8",$path);
                echo $nval.'<br>';
                $this->addEmptyDir($path);
                $nodes = glob($path.'/*');
                foreach ($nodes as $node) {
                	$nnode = iconv("GBK", "UTF-8",$node);
                    echo $nnode . '<br>';
                    if (is_dir($node)) {
                        $this->addDir2($node);
                    }elseif(is_file($node)){
                        $this->addFile($node);
                    }
                }
            }
        }
        $zip = new Zipper;
        $res = $zip->open($_SESSION['folder'].'backup.zip', ZipArchive::CREATE);
        if ($res === TRUE) {
        	$f = substr($_SESSION['folder'], 0, -1);
        	$f = substr($f, 2);
            $zip->addDir($f);
            $zip->close();
            echo "压缩完成<br>你可以 <a href=\"".$_SERVER['SCRIPT_NAME']."?op=home&folder=".$_SESSION['folder']."\">查看文件夹</a> 或者 <a href=\"".$_SERVER['SCRIPT_NAME']."?op=home\">返回文件管理</a>\n";
        }else{
            echo '<span class="error">压缩失败！</span>'
                ." <a href=\"".$_SERVER['SCRIPT_NAME']."?op=home&folder=".$_SESSION['folder']."\">返回文件管理</a>\n";
        }
        mainbottom();
    }else{
    	printerror('此服务器上的PHP不支持ZipArchive，无法压缩文件！');
    }
    }else{
        printerror("您没有选择文件");
    }
}

if(@$_POST['action']=='权限'){
    if(isset($_POST['select_item'])){
    	maintop("修改权限");
    	$chmod = octdec($_REQUEST['chmod']);
        function ChmodMine($file, $chmod)
        {
        	$nfile = $file;
        	$file = iconv("UTF-8", "GBK",$file);
        	if(is_file($file)){
                if(@chmod($file, $chmod)){
                	echo $nfile.' 权限修改成功<br>';
                }else{
                	echo '<span class="error">'.$nfile.' 权限修改失败</span><br>';
                }
        	}elseif(is_dir($file)){
                if(@chmod($file, $chmod)){
                	echo $nfile.' 权限修改成功<br>';
                }else{
                	echo '<span class="error">'.$nfile.' 权限修改失败</span><br>';
                }
        		$foldersAndFiles = @scandir($file);
        		$entries = @array_slice($foldersAndFiles, 2);
        		foreach($entries as $entry){
        			$nentry = iconv("GBK", "UTF-8",$entry);
        			ChmodMine($nfile.'/'.$nentry, $chmod);
        		}
        	}else{
        		echo '<span class="error">'.$nfile.' 文件不存在！</span><br>';
        	}
        }
        if(@$_POST['select_item']['d']){
            foreach($_POST['select_item']['d'] as $val){
                ChmodMine($val,$chmod);
            }
        }
        if(@$_POST['select_item']['f']){
            foreach($_POST['select_item']['f'] as $val){
                ChmodMine($val,$chmod);
            }
        }
        echo "<a href=\"".$_SERVER['SCRIPT_NAME']."?op=home&folder=".$_SESSION['folder']."\">返回文件管理</a>\n";
        mainbottom();
    }else{
        printerror("您没有选择文件");
    }
}

/****************************************************************/
/* function switch()                                            */
/*                                                              */
/* Switches functions.                                          */
/* Recieves $op() and switches to it                            *.
/****************************************************************/

switch($op) {

    case "home":
    home();
    break;

    case "up":
    up();
    break;

    case "yupload":
    if(!isset($_REQUEST['url'])){
    	printerror('您没有输入文件地址！');
    }elseif(isset($_REQUEST['ndir'])){
        yupload($_REQUEST['url'], $_REQUEST['ndir'], @$_REQUEST['unzip'] ,@$_REQUEST['delzip']);
    }else{
    	yupload($_REQUEST['url'], './', @$_REQUEST['unzip'] ,@$_REQUEST['delzip']);
    }
    break;

    case "upload":
    if(!isset($_FILES['upfile'])){
    	printerror('您没有选择文件！');
    }elseif(isset($_REQUEST['ndir'])){
        upload($_FILES['upfile'], $_REQUEST['ndir'], @$_REQUEST['unzip'] ,@$_REQUEST['delzip']);
    }else{
    	upload($_FILES['upfile'], './', @$_REQUEST['unzip'] ,@$_REQUEST['delzip']);
    }
    break;

    case "unz":
    unz($_REQUEST['dename']);
    break;

    case "unzip":
    unzip($_REQUEST['dename'],$_REQUEST['ndir'],@$_REQUEST['del']);
    break;

    case "sqlb":
    sqlb();
    break;

    case "sqlbackup":
    sqlbackup($_POST['ip'], $_POST['sql'], $_POST['username'], $_POST['password']);
    break;

    case "ftpa":
    ftpa();
    break;

    case "ftpall":
    ftpall($_POST['ftpip'], $_POST['ftpuser'], $_POST['ftppass'], $_POST['goto'], $_POST['ftpfile'], $_POST['del']);
    break;

    case "allz":
    allz();
    break;

    case "allzip":
    allzip();
    break;

    case "edit":
    edit($_REQUEST['fename']);
    break;

    case "save":
    save($_REQUEST['ncontent'], $_REQUEST['fename']);
    break;

    case "cr":
    cr();
    break;

    case "create":
    create($_REQUEST['nfname'], $_REQUEST['isfolder'], $_REQUEST['ndir']);
    break;

    case "ren":
    ren($_REQUEST['file']);
    break;

    case "rename":
    renam($_REQUEST['rename'], $_REQUEST['nrename'], $folder);
    break;

    case "movall":
    movall(@$_REQUEST['file'], @$_REQUEST['ndir'], $folder);
    break;

    case "copy":
    tocopy(@$_REQUEST['file'], @$_REQUEST['ndir'], $folder);
    break;

    case "printerror":
    printerror($error);
    break;

    case "logout":
    logout();
    break;   

    case "z":
    z($_REQUEST['dename'],$_REQUEST['folder']);
    break;

    case "zip":
    zip($_REQUEST['dename'],$_REQUEST['folder']);
    break;

    case "killme":
    killme($_REQUEST['dename']);
    break;

    default:
    home();
    break;
}

?>